<?php

namespace App\Admin\Controllers;

use App\Models\AreaInfo;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

use Encore\Admin\Facades\Admin;

use App\Admin\Actions\Post\OpenAllPost;
use App\Admin\Actions\Post\CloseAllPost;

class ItemController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '展项控制';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new AreaInfo);
        // 全部关闭
        $grid->disableActions();
        $grid->disableFilter();
        $grid->disableCreateButton();
        $grid->disablePagination();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new OpenAllPost());
            $tools->append(new CloseAllPost());
        });

        $grid->column('id', __('序号'));
        $grid->column('name', __('名称'));
        $grid->column('desc', __('描述'));
        $grid->column('', __('状态'));

        $grid->column(' ', __('开关操作'))->display(function () {
            return "<a class='btn btn-success btn-sm item-row-on' data-id='{$this}'>开启</a>  <a class='btn btn-danger btn-sm item-row-off' data-id='{$this}'>关闭</a>";
        });
        $this->script = <<<EOT

        //open all
        $('.openall-posts').unbind('click').click(function() {
            swal({
                title: "确认打开全部的电源吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {

                    $.ajax({
                        method: 'get',
                        url: '/api/getDevices',
                        success: function (data) {
                            // console.log(data);
                            var o = eval("(" + data + ")");  // 转换为json对象
                            console.log(o.data);

                            var i = 0;
                            var t = setInterval(function() {
                                var _id = o.data[i].id;
                                var _name = o.data[i].name;
                                var _cmd = o.data[i].cmd;
                                $.ajax({
                                    method: 'get',
                                    url: '/api/sentCmd?id=' + _id + '&flag=12&cmd=' + _cmd,
                                    success: function (data) {
                                        // console.log(data);
                                        var o = eval("(" + data + ")");  // 转换为json对象
                                        // console.log(o.code);
                                        
                                        if(o.code == '200') {
                                            $.admin.toastr.success('打开' + _name + '的电源成功！', '', {positionClass:"toast-top-center"});
                                        }else{
                                            $.admin.toastr.error('打开' + _name + '的电源失败！', '', {positionClass:"toast-top-center"});
                                        }
                                        

                                    }
                                }); 
                                i += 1;
                                if(i > o.data.length - 1){
                                    clearInterval(t);
                                    $.pjax.reload('#pjax-container');
                                }
                            },3000);


                        }
                    });
                }
            });
        });

        //open all
        $('.closeall-posts').unbind('click').click(function() {
            swal({
                title: "确认关闭全部的电源吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    // $.ajax({
                    //     method: 'get',
                    //     url: '/api/controlAll?flag=11',
                    //     success: function (data) {
                    //         console.log(data);
                    //         var o = eval("(" + data + ")");  // 转换为json对象
                    //         console.log(o.code);
                            
                    //         if(o.code == '200') {
                    //             swal('关闭全部电源成功！', '', 'success');
                    //         }else{
                    //             swal('关闭全部电源失败！', '', 'error');
                    //         }
                    //         $.pjax.reload('#pjax-container');

                    //     }
                    // });
                    $.ajax({
                        method: 'get',
                        url: '/api/getDevices',
                        success: function (data) {
                            // console.log(data);
                            var o = eval("(" + data + ")");  // 转换为json对象
                            console.log(o.data);

                            var i = 0;
                            var t = setInterval(function() {
                                var _id = o.data[i].id;
                                var _name = o.data[i].name;
                                var _cmd = o.data[i].cmd;
                                $.ajax({
                                    method: 'get',
                                    url: '/api/sentCmd?id=' + _id + '&flag=11&cmd=' + _cmd,
                                    success: function (data) {
                                        // console.log(data);
                                        var o = eval("(" + data + ")");  // 转换为json对象
                                        // console.log(o.code);
                                        
                                        if(o.code == '200') {
                                            $.admin.toastr.success('关闭' + _name + '的电源成功！', '', {positionClass:"toast-top-center"});
                                        }else{
                                            $.admin.toastr.error('关闭' + _name + '的电源失败！', '', {positionClass:"toast-top-center"});
                                        }
                                    }
                                }); 
                                i += 1;
                                if(i > o.data.length - 1){
                                    clearInterval(t);
                                    $.pjax.reload('#pjax-container');
                                }
                            },2000);

                            
                        }
                    });
                }
            });
        });


        //open item all
        $('.item-row-on').unbind('click').click(function() {
            // console.log($(this).data('id'));
            var _item_id = $(this).data('id').id;
            var _item_name = $(this).data('id').name;
            swal({
                title: "确认打开" + _item_name + "全部的电源吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    $.ajax({
                        method: 'get',
                        url: '/api/getItemDevices?item_id=' + _item_id,
                        success: function (data) {
                            // console.log(data);
                            var o = eval("(" + data + ")");  // 转换为json对象
                            console.log(o.data);

                            var i = 0;
                            var t = setInterval(function() {
                                var _id = o.data[i].id;
                                var _name = o.data[i].name;
                                var _cmd = o.data[i].cmd;
                                $.ajax({
                                    method: 'get',
                                    url: '/api/sentCmd?id=' + _id + '&flag=12&cmd=' + _cmd,
                                    success: function (data) {
                                        // console.log(data);
                                        var o = eval("(" + data + ")");  // 转换为json对象
                                        // console.log(o.code);
                                        
                                        if(o.code == '200') {
                                            $.admin.toastr.success('打开' + _item_name + '的' + _name + '的电源成功！', '', {positionClass:"toast-top-center"});
                                        }else{
                                            $.admin.toastr.error('打开' + _item_name + '的' + _name + '的电源失败！', '', {positionClass:"toast-top-center"});
                                        }
                                        

                                    }
                                }); 
                                i += 1;
                                if(i > o.data.length - 1){
                                    clearInterval(t);
                                    $.pjax.reload('#pjax-container');
                                }
                            },3000);

                        }
                    });
                }
            });
        });

        //close item all
        $('.item-row-off').unbind('click').click(function() {
            // console.log($(this).data('id'));
            var _item_id = $(this).data('id').id;
            var _item_name = $(this).data('id').name;
            swal({
                title: "确认关闭" + _item_name + "全部的电源吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    $.ajax({
                        method: 'get',
                        url: '/api/getItemDevices?item_id=' + _item_id,
                        success: function (data) {
                            // console.log(data);
                            var o = eval("(" + data + ")");  // 转换为json对象
                            console.log(o.data);

                            var i = 0;
                            var t = setInterval(function() {
                                var _id = o.data[i].id;
                                var _name = o.data[i].name;
                                var _cmd = o.data[i].cmd;
                                $.ajax({
                                    method: 'get',
                                    url: '/api/sentCmd?id=' + _id + '&flag=11&cmd=' + _cmd,
                                    success: function (data) {
                                        // console.log(data);
                                        var o = eval("(" + data + ")");  // 转换为json对象
                                        // console.log(o.code);
                                        
                                        if(o.code == '200') {
                                            $.admin.toastr.success('关闭' + _item_name + '的' + _name + '的电源成功！', '', {positionClass:"toast-top-center"});
                                        }else{
                                            $.admin.toastr.error('关闭' + _item_name + '的' + _name + '的电源失败！', '', {positionClass:"toast-top-center"});
                                        }
                                        

                                    }
                                }); 
                                i += 1;
                                if(i > o.data.length - 1){
                                    clearInterval(t);
                                    $.pjax.reload('#pjax-container');
                                }
                            },1000);

                        }
                    });
                }
            });
        });
        
EOT;
        Admin::script($this->script);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(AreaInfo::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('desc', __('Desc'));
        $show->field('updated_at', __('Updated at'));
        $show->field('created_at', __('Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new AreaInfo);

        $form->text('name', __('Name'));
        $form->text('desc', __('Desc'));

        return $form;
    }
}
